#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
    int searchInsert(vector<int> &nums, int target)
    {
        int length = nums.size();
        int mid = length / 2;
        while (nums[mid] != target)
        {
            // 陷入死循环但会自己跳出
            cout << "1";
            if (nums[mid] < target)
            {
                if (mid == length)
                    return mid + 1;
                    // 这里nums[mid+1]越界了，且会导致死循环，但运行时会自动跳出，可能是编译器原因，debug时则会进入死循环
                if (nums[mid + 1] > target)
                    return mid + 1;
                cout << nums[mid + 1] << endl;
                cout << nums[mid] << endl;
                mid = (mid + length) / 2;
                continue;
            }
            else if (mid == 0)
                return mid;
            else if (nums[mid - 1] < target)
            {
                return mid;
            }

            mid = mid / 2;
        }
        return mid;
    }
};

int main()
{
    Solution test;
    vector<int> nums = {1, 3, 5, 6};
    int target = 7;
    cout << test.searchInsert(nums, target);
}